﻿
Option Explicit On
'*******************************************************************
' Program: pkgVisio
' Author: Albert E Edlund
' Date:
'
' Purpose:
' Working with office object XML
'
'*******************************************************************


#Region " imports "

Imports System
Imports System.Object
Imports System.IO
Imports System.IO.TextReader
' need to reference windowsbase in .net
Imports System.IO.Packaging

' need for dbconnectionstringbuilder used in clsConnections
Imports System.Data.Common

Imports System.Windows
Imports System.Windows.Forms
Imports System.Text.StringBuilder

Imports Microsoft.VisualBasic

Imports System.Text
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Imports System.Xml
Imports System.Linq
Imports System.Linq.Enumerable
Imports System.Windows.Input

Imports System.Xml.Serialization
Imports System.Xml.Linq
Imports System.Xml.XPath

Imports ADODB
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Sql


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging


' visio v15 does not use a xName prefix which may put it into conflict with other open office apps
Imports <xmlns="http://schemas.microsoft.com/office/visio/2012/main">
Imports <xmlns:rs="urn:schemas-microsoft-com:rowset">

' generic namespace for office open xml, used in all of them
Imports <xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">

Imports pkgVisio.clsVisPackage
Imports pkgVisio.clsStructures

#End Region



'**********************************************************************************
'
'**********************************************************************************


Public Class frmWindows



#Region " Properties and Declarations "

    '
    ' what is the latest document
    '
    Public _xDoc As XDocument
    Public Property xDoc As XDocument
        Get
            Return Me._xDoc
        End Get
        Set(value As XDocument)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "xDoc is Null.")
            End If
            Me._xDoc = value
        End Set
    End Property

    '
    '
    '
    Public m_visPackage As clsVisPackage
    Public Property visPackage As clsVisPackage
        Get
            visPackage = Me.m_visPackage
        End Get
        Set(value As clsVisPackage)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "visPackage is Null.")
            End If
            Me.m_visPackage = value
        End Set
    End Property

    '
    '
    '
    Private m_visWindows As clsWindows
    Public Property visWindows As clsWindows
        Get
            visWindows = Me.m_visWindows
        End Get
        Set(value As clsWindows)
            Me.m_visWindows = value
        End Set
    End Property


#End Region

    '
    '
    '
    Private Sub HelpToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles HelpToolStripMenuItem.Click
        frmAbout.ShowDialog()
    End Sub

    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub


    '
    '
    '
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.m_visPackage = Nothing
        Me.visPackage = New clsVisPackage

        Me.m_visWindows = Nothing
        Me.visWindows = New clsWindows(Me.visPackage)


    End Sub

    '
    '
    '
    Private Sub frmWindows_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown

        Me.m_visWindows = Nothing
        Me.visWindows = New clsWindows(Me.visPackage)

        Me.lvWindows.Clear()
        Me.lvWinAttr.Clear()
        Me.lvWinElem.Clear()

        Dim colPkgParts As Collection(Of pkgpartInfo) = New Collection(Of pkgpartInfo)
        colPkgParts = Me.visPackage.getPkgPartsByContent(pkgVisio.clsVisPackage.contXmlWindows)
        If colPkgParts.Count = 1 Then
            Me.visWindows.xDoc = Me.visPackage.getPackagePartAsXDoc(Me.visPackage.fileCurrent, colPkgParts(0))
            Me.visWindows.loadWindows(Me.lvWindows)
        End If


    End Sub

    '
    '
    '
    Private Sub lvWindows_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvWindows.SelectedIndexChanged

        Dim winIndices As ListView.SelectedIndexCollection = Me.lvWindows.SelectedIndices
        Dim winItems As ListView.SelectedListViewItemCollection = Me.lvWindows.SelectedItems
        Dim winItem As ListViewItem

        Try


            If 0 < winItems.Count Then
                winItem = winItems(0)


                Dim qryWindow = From winelem In Me.visWindows.xDoc...<Window>
                   Where winelem.@ID = winItem.Text

                If qryWindow.Count = 1 Then
                    Dim tempDoc As XDocument = Nothing
                    Using stringReader As New StringReader(qryWindow.First.ToString)
                        tempDoc = XDocument.Load(XmlReader.Create(stringReader))
                        Me.visPackage.readAttAndElem(tempDoc, "", "Window")
                        Me.visPackage.loadAttElemLV(Me.lvWinAttr, Me.visPackage.attDict)
                        Me.visPackage.loadAttElemLV(Me.lvWinElem, Me.visPackage.elemDict)
                    End Using

                End If ' test for query count
            End If ' test for selected count

        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


End Class